﻿(function ($) {
	$.fn.pager = function (options) {
		var opts = $.extend({}, $.fn.pager.defaults, options);
		return this.each(function () {
			$(this).empty().append(renderpager(parseInt(options.pagenumber), parseInt(options.pagecount), options.buttonClickCallback));
			$(".pagination li").mouseover(function () {
				document.body.style.cursor = "pointer";
			}).mouseout(function () {
				document.body.style.cursor = "auto";
			});
		});
	};
	function renderpager(pagenumber, pagecount, buttonClickCallback) {
		var $pager = $("<ul class='pagination'></ul>");
		$pager.append(renderButton("<<", pagenumber, pagecount, buttonClickCallback)).append(renderButton("<", pagenumber, pagecount, buttonClickCallback));
		var startPoint = 1;
		var endPoint = 9;
		if (pagenumber > 4) {
			startPoint = pagenumber - 4;
			endPoint = pagenumber + 4;
		}
		if (endPoint > pagecount) {
			startPoint = pagecount - 8;
			endPoint = pagecount;
		}
		if (startPoint < 1) {
			startPoint = 1;
		}
		for (var page = startPoint; page <= endPoint; page++) {
			var currentButton = $("<li><a href='javascript:void(0)'>" + (page) + "</a></li>");
			page == pagenumber ? currentButton.addClass("active") : currentButton.click(function () {
				buttonClickCallback(this.firstChild.firstChild.data);
			});
			currentButton.appendTo($pager);
		}
		$pager.append(renderButton(">", pagenumber, pagecount, buttonClickCallback)).append(renderButton(">>", pagenumber, pagecount, buttonClickCallback));
		return $pager;
	}	
	function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) {
		//pgNext 下一步 上一步
		var $Button = $("<li><a href='javascript:void(0)'>" + buttonLabel + "</a></li>");
		var destPage = 1;
		switch (buttonLabel) {
		  case "<<":
			destPage = 1;
			break;
		  case "<":
			destPage = pagenumber - 1;
			break;
		  case ">":
			destPage = pagenumber + 1;
			break;
		  case ">>":
			destPage = pagecount;
			break;
		}
        // disable and 'grey' out buttons if not needed.
		if (buttonLabel == "<<" || buttonLabel == "<") {
			//灰掉按钮pgEmpty
			pagenumber <= 1 ? $Button.addClass("disabled") : $Button.click(function () {
				buttonClickCallback(destPage);
			});
		} else {
			pagenumber >= pagecount ? $Button.addClass("disabled") : $Button.click(function () {
				buttonClickCallback(destPage);
			});
		}
		return $Button;
	}
	$.fn.pager.defaults = {pagenumber:1, pagecount:1};
})(jQuery);


var is={ie:navigator.appName=='Microsoft Internet Explorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'}
is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;}
if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}